@using RazorLight
@inherits TemplatePage<RestApiSpec>
@using System
@using System.Collections.Generic
@using System.Globalization
@using System.Linq
@using System.Text.RegularExpressions
@using ApiGenerator.Domain
@using ApiGenerator 
@using CsQuery.StringScanner.Implementation
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Elasticsearch.Net;
using static Elasticsearch.Net.HttpMethod;

//This file is automatically generated from https://github.com/elastic/elasticsearch/tree/master/rest-api-spec

@{
	RestApiSpec model = Model;
<text>
namespace Elasticsearch.Net
{
	///<summary>
	///Raw operations with elasticsearch
	///</summary>
	public partial class ElasticLowLevelClient : IElasticLowLevelClient
	{
</text>
	foreach (var kv in model.Endpoints)
	{
		var endpoint = kv.Value;
		var methods = endpoint.GetCsharpMethods().ToList();
		var partsNotToReplace = new HashSet<string> {"category_id"};	
		foreach (var method in methods)
		{
			var url = method.Path.Split(new[] {'/'}, 2).Last();
			// Don't replace certain url parts e.g. category_id is defined as a long parameter
			// which is .ToString()'ed as part of the URL. A .NotNull() method on long doesn't really make sense.
			var partNames = method.Url.Parts != null
				? string.Join("|", method.Url.Parts.Where(p => !partsNotToReplace.Contains(p.Key)).Select(p => p.Key))
				: @"\w+";
			var pattern = string.IsNullOrEmpty(partNames) ? @"\w+" : partNames;
			var patchedUrl = Regex.Replace(url, "{(" + pattern + ")}", "{$1.NotNull(\"$1\")}");
			bool isAsync = method.ReturnType.StartsWith("Task<");
			string requestMethod = isAsync ? "DoRequestAsync" : "DoRequest";
<text>		///<summary>@method.HttpMethod on @method.Path <para>@method.Documentation</para></summary>
</text>
			foreach (var part in method.Parts)
			{
<text>		@Raw("///<param name=\"" + part.Name + "\">")@part.Description@Raw("</param>")
</text>
			}
<text>		@Raw(@"///<param name=""requestParameters"">A func that allows you to describe the querystring parameters &amp; request specific connection settings.</param>")
</text>
		if (!string.IsNullOrEmpty(method.ObsoleteMethodVersion))
		{
<text>		[Obsolete("Scheduled to be removed in 7.0, @method.ObsoleteMethodVersion removed this method from the rest specification")]
</text>}
<text>		public @Raw(method.ReturnType) @(method.FullName)@(Raw(method.ReturnTypeGeneric))(@Raw(method.Arguments))
			where @method.CallTypeGeneric : class, IElasticsearchResponse, new() => this.@(requestMethod)@(Raw("<" + method.CallTypeGeneric +">"))(@method.HttpMethod, Url($"@(Raw(patchedUrl))"), @(method.FullName.Contains("Async") ? "ctx, " : string.Empty)@(method.Parts.Any(pp=>pp.Name == "body") ? "body" : "null"), _params(requestParameters@(Raw(url.StartsWith("_cat") ? ", contentType: \"text/plain\", accept: \"text/plain\"" : ""))));
</text>
		}
	}
	<text>
	}
	}
	</text>
}
